package com.cj.web.back;

import com.cj.entity.Coursedetail;
import com.cj.service.CourseDetailService;
import com.cj.utils.FileNameUtils;
import com.cj.utils.JSONUtils;
import com.cj.utils.ResultVO;
import com.cj.web.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/courseDetail")
public class CourseDetailServlet extends BaseServlet {

    ResultVO vo;
    CourseDetailService service = new CourseDetailService();
    public void  addCourseDetail(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        Coursedetail courseDetail = new Coursedetail();
        BeanUtils.populate(courseDetail,map);
//        处理文件名
        String url = req.getParameter("url");
        String s = FileNameUtils.substringFileName(url);
        courseDetail.setUrl(s);

        System.out.println(courseDetail);

//        调用service
        int row = service.insertCourseDetail(courseDetail);

        if (row >0){
            vo = new ResultVO(200,"添加课程详细成功",null);
        }else{
            vo = new ResultVO(500,"添加课程详细失败",null);
        }
        JSONUtils.objToJson(vo,resp);
    }
}
